package com.icebartech.core.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 文件内容解析工具类
 * @author Anler
 *
 */
public class FileReadUtil {

	private static Logger logger = LoggerFactory.getLogger(FileReadUtil.class);
	
	public static List<String> readTxtOrCvs(File file) {
		List<String> dataList = new ArrayList<String>();
		LineIterator li       = null;
		try {
			li = FileUtils.lineIterator(file);
			while (li.hasNext()) {
				dataList.add(li.nextLine());
			}
			LineIterator.closeQuietly(li);
		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			if (li != null) {
				try {
					LineIterator.closeQuietly(li);
					li = null;
				} catch (Exception e) {
					logger.error(e.getMessage());
				}
			}
		}
		return dataList;
	}

	public static List<String> readXls1(File file) {
		List<String> dataList = new ArrayList<String>();
		try {
			String filePath = file.getName();
			String fileType = filePath.substring(filePath.lastIndexOf(".") + 1, filePath.length());
			if (fileType.equals("xls") || fileType.equals("xlsx")) {
				InputStream stream = new FileInputStream(file);
				Workbook wb = WorkbookFactory.create(stream);
				Sheet sheet = wb.getSheetAt(0);
				for (Row row : sheet) {
					for (Cell cell : row) {
						cell.setCellType(CellType.STRING);	// 将单元格内容格式设为String
						dataList.add(cell.getStringCellValue());
					}
				}
			} else {
				logger.error("您输入的excel格式不正确");
			}
		} catch (Exception e) {
			logger.error("读取文件内容出错");
			e.printStackTrace();
		}
		return dataList;
	}

	public static Map<String, List<List<String>>> readXls(File file) {
		Map<String, List<List<String>>> sheetData = new HashMap<String, List<List<String>>>();
		List<List<String>> rowData;
		List<String> cellData;
		try {
			String filePath = file.getName();
			String fileType = filePath.substring(filePath.lastIndexOf(".") + 1, filePath.length());
			if (fileType.equals("xls") || fileType.equals("xlsx")) {
				InputStream stream = new FileInputStream(file);
				Workbook wb = WorkbookFactory.create(stream);
				// 获取sheet页数
				int sheetNum = wb.getNumberOfSheets();
				for (int i = 0; i < sheetNum; i++) {
					// 获取每一页sheet
					Sheet sheet = wb.getSheetAt(i);
					// 获取当前sheet的名称
					String sheetName = wb.getSheetName(i);
					rowData = new ArrayList<>();
					// 获取sheet下的每一行row
					for (int j = 0; j < sheet.getLastRowNum()+1; j++) {
						cellData = new ArrayList<>();
						Row row = sheet.getRow(j);
						if (null != row) {
							// 获取row下的每一个单元格cell
							boolean allCellIsNull = true;
							for (int k = 0; k < sheet.getRow(0).getLastCellNum(); k++) {
								// 保存row下的每一个单元格cell
								Cell cell = row.getCell(k);
								if (null == cell) {
									cellData.add(null);
									continue;
								}
								cell.setCellType(CellType.STRING);	// 将单元格内容格式设为String
								String cellValue = cell.getStringCellValue();
								if (StringUtils.isBlank(cellValue)) {
									cellData.add(null);
									continue;
								}
								// 本行有单元格有内容
								allCellIsNull = false;
								cellData.add(cellValue);
							}
							if(allCellIsNull) cellData = new ArrayList<>(); // 空行
						}
						rowData.add(cellData);
					}
					// 保存sheet下的每一行row
					sheetData.put(sheetName, rowData);
				}
			} else {
				logger.error("excel格式不正确");
			}
		} catch (Exception e) {
			logger.error("读取文件内容出错");
			e.printStackTrace();
		}
		return sheetData;
	}

	/*public static void main(String[] args) {

		File xlsFile = new File("C:/Users/Anler/Desktop/order_import_template.xls");
		Map<String, List<List<String>>> sheetData = readXls(xlsFile);
		for (String key : sheetData.keySet()) {
			logger.info(key + "：");
			List<List<String>> rowData = sheetData.get(key);
			for (int i = 0; i < rowData.size(); i++) {
				logger.info("==================================");
				List<String> cellData = rowData.get(i);
				for (int j = 0; j < cellData.size(); j++) {
					logger.info("(" + CommonUtil.toNumberSystem26(j+1) + (i+1) + ")");
					logger.info(cellData.get(j));
				}
			}
		}*/

	/*}*/

}
